package com.hdax.dm.user.controller;

import com.hdax.dm.user.service.ContactService;
import com.hdax.dm.user.service.UserService;
import com.hdax.dm.user.vo.UserVo;
import com.hdax.dm.utils.CommonResponse;
import com.hdax.dm.utils.ResponseUtil;
import com.hdax.dm.utils.Token;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 获取常用联系人
 */
@RestController
@RequestMapping("/user")
public class ContactController {
    @Autowired
    private ContactService contactService;
    @Autowired
    private Token tokens;
    @Autowired
    private UserService userService;

   /* *//**
     * 获取全部联系人
     * @param request
     * @return
     *//*
    @PostMapping("order/ticketbuyerlist")
    public CommonResponse ticketbuyerlist(HttpServletRequest request) {
        // 获取请求消息中的所有头字段
        String token = request.getHeader("token");
        //解密
        Claims claims = Token.token(token,RSA_PUBLIC_KEY);
        Long userId = Long.valueOf(claims.getId());

        return ResponseUtil.returnSuccess(contactService.list(userId));
    }*/
    //添加用户常用联系人
    @PostMapping("/order/addticketbuyer")
    public CommonResponse addticketbuyer(@RequestBody Map<String, String> param, HttpServletRequest request){
        // 获取请求消息中的所有头字段
        String token = request.getHeader("token");
        //解密
        Long id = Long.parseLong(tokens.token(token));
        return  contactService.addContact(id,param);
    }

    /**
     *验证购票人是否存在
     * @return
     */
    @PostMapping("/order/validatebuyerexist")
    public CommonResponse validatebuyerexist(@RequestBody Map<String, String> param) throws Exception {
        //获取身份者信息
        String idCard = param.get("idCard");
        return contactService.isNotContact(idCard);
    }


    /**
     * 删除常用联系人
     * @return
     */
    @PostMapping("/order/deleteticketbuyer")
    public CommonResponse deleteticketbuyer(@RequestBody Map<String, Long> param){
        //获取id
        Long id = param.get("id");
        return contactService.delectContact(id);
    }

    //根据用户编号修改用户信息
    @PostMapping(path = "/p/modifypersoninfo")
    public CommonResponse modifyPersonInfo(HttpServletRequest request, @RequestBody UserVo vo){
        // 获取请求消息中的所有头字段
        String  token  = request.getHeader("token");
        //解密
        String claims = tokens.token(token);
        Long userId = Long.valueOf(claims);
        System.out.println("用户编号"+userId+"其他信息"+vo.toString());
        return ResponseUtil.returnSuccess(userService.modifyPersonInfo(userId,vo));
    }
}